//
//  ComposeViewController.swift
//  weibo117
//
//  Created by 宋禹奇 on 15/7/6.
//  Copyright © 2015年 宋禹奇. All rights reserved.
//

import UIKit
import FFAutoLayout

class ComposeViewController: UIViewController {
    
    override func loadView() {
        view = UIView()
        view.backgroundColor = UIColor.whiteColor()
        //准备导航栏
        prepareNavigationBar()
        //准备文本框
        prepareTextView()
        
    }
    //切换表情视图
    func inputEmoticon() {
        print("输入表情")
    }
    
    //选择照片
    func selectPicture() {
        print("选择照片")
    }

    //关闭
    func close(){
        view.endEditing(true)
        dismissViewControllerAnimated(true, completion: nil)
    
    }
    //发布微博
    func sendStatus() {
    
    
    }
    
    private func prepareToolBar() -> UIToolbar {
    
        let toolBar = UIToolbar(frame: CGRectMake(0, 0, 0, 44))
        toolBar.backgroundColor = UIColor(white: 0.8, alpha: 1.0)
        
        // 创建按钮数组
        let itemSettings = [["imageName": "compose_toolbar_picture", "action": "selectPicture"],
            ["imageName": "compose_mentionbutton_background"],
            ["imageName": "compose_trendbutton_background"],
            ["imageName": "compose_emoticonbutton_background", "action": "inputEmoticon"],
            ["imageName": "compose_addbutton_background"]]

        //实例化toolbar 按钮数组
        var items = [UIBarButtonItem]()
        
        //遍历所有按钮
        for settings in itemSettings {
        
            items.append(UIBarButtonItem(imageName: settings["imageName"]!, highlightedImageName: nil, target: nil, actionName: settings["action"]))
            
            //添加弹簧
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
        
        }
        
        //删除末尾的弹簧
        items.removeLast()
        
        toolBar.items = items
        
        return toolBar
    
    }
    
    //准备文本框
    private func prepareTextView() {
    
        view.addSubview(textView)
        
        textView.text = "分享新鲜事....."
        textView.font = UIFont.systemFontOfSize(18)
        textView.ff_Fill(view)
        
        //打开垂直弹簧效果
        textView.alwaysBounceVertical = true
        
        //自定义inputAccessoryView
        textView.inputAccessoryView = prepareToolBar()
        
        //滚动关闭键盘
        textView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        
        //设置占位标签
        placeHolderLabel.text = "分享新鲜事....."
        placeHolderLabel.sizeToFit()
        textView.addSubview(placeHolderLabel)
        placeHolderLabel.ff_AlignInner(ff_AlignType.TopLeft, referView: textView, size: nil, offset: CGPoint(x: 5, y: 8))
        
    
    
    }
    
    //准备导航栏
    private func prepareNavigationBar() {
        //创建左侧按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        
        
        //创建右侧按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendStatus")
        navigationItem.rightBarButtonItem?.enabled = false
        
        //创建中间的labelView
        let titleView = UIView(frame: CGRectMake(0, 0, 100, 32))
        
        let label1 = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        label1.text = "发微博"
        label1.sizeToFit()
        
        let label2 = UILabel(color: UIColor(white: 0.0, alpha: 0.5), fontSize: 13)
        label2.text = sharedUserAccount?.name ?? ""
        label2.sizeToFit()
        
        titleView.addSubview(label1)
        titleView.addSubview(label2)
        
        
        label1.ff_AlignInner(ff_AlignType.TopCenter, referView: titleView, size: nil)
        label2.ff_AlignInner(ff_AlignType.BottomCenter, referView: titleView, size: nil)
        
        navigationItem.titleView = titleView
        
        
    }
    
    //MARK: 懒加载
    private lazy var textView = UITextView()
    private lazy var placeHolderLabel = UILabel(color: UIColor.lightGrayColor(), fontSize: 18)

}
